package gui;

import generated.domain.user.User;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import vworld.Client;

public class RegisterDialog {

	private Display display;

	private boolean isValid = false;

	private Shell shell;

	public RegisterDialog(Display display) {
		this.display = display;
		createContents();
	}

	private void createContents() {
		shell = new Shell(display, SWT.CLOSE);
		shell.setText("Create an account");

		GridData lGridData = new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL);
		lGridData.widthHint = 150;

		Group registerGroup = new Group(shell, SWT.NONE);
		registerGroup.setText("New account");
		registerGroup.setLayout(new GridLayout(2, false));

		Label nameLabel = new Label(registerGroup, SWT.NONE);
		nameLabel.setText("Name");
		final Text nameText = new Text(registerGroup, SWT.SINGLE | SWT.BORDER);
		nameText.setLayoutData(lGridData);
		nameText.setFocus();

		Label emailLabel = new Label(registerGroup, SWT.NONE);
		emailLabel.setText("Email");
		final Text emailText = new Text(registerGroup, SWT.SINGLE | SWT.BORDER);
		emailText.setLayoutData(lGridData);

		Label loginLabel = new Label(registerGroup, SWT.NONE);
		loginLabel.setText("Login");
		final Text loginText = new Text(registerGroup, SWT.SINGLE | SWT.BORDER);
		loginText.setLayoutData(lGridData);

		Label passwordLabel = new Label(registerGroup, SWT.NONE);
		passwordLabel.setText("Password");
		final Text passText = new Text(registerGroup, SWT.PASSWORD | SWT.BORDER);
		passText.setLayoutData(lGridData);

		Label hostLabel = new Label(registerGroup, SWT.NONE);
		hostLabel.setText("Host name");
		final Text hostText = new Text(registerGroup, SWT.SINGLE | SWT.BORDER);
		hostText.setLayoutData(lGridData);
		hostText.setText("localhost");

		Label portLabel = new Label(registerGroup, SWT.NONE);
		portLabel.setText("Port");
		final Text portText = new Text(registerGroup, SWT.SINGLE | SWT.BORDER);
		portText.setLayoutData(lGridData);
		portText.setText("900");

		Button createButton = new Button(shell, SWT.PUSH);
		createButton.setText("Create");
		createButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));

		createButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				Client.ORB = null;
				if (Client.ORB == null) {
					Client.initORB(hostText.getText(), portText.getText());
				}

				try {
					User newUser = Client.USER_SERVICE.createUser(nameText
							.getText(), "simple user", emailText.getText(),
							loginText.getText(), passText.getText());
					if (newUser != null && !newUser.role.equals("")) {
						MessageDialog.openInformation(shell, "Register User",
								"User successfully register");
						// Client.ORB.disconnect(Client.ORB);
						Client.ORB = null;
						shell.dispose();
					} else {
						MessageDialog.openError(shell, "Register Failure",
						"This login is already used. Choose another one!");
					}
				} catch (NullPointerException e) {
					MessageDialog.openError(shell, "Register Failure",
							"User cannot be register");
				}
			}
		});
		shell.setDefaultButton(createButton);

		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginWidth = 10;
		gridLayout.marginHeight = 10;
		shell.setLayout(gridLayout);

		shell.pack();
		shell.setMaximized(false);
		shell.open();
		shell.layout();

		// center on the screen
		Rectangle displayBounds = Display.getCurrent().getBounds();
		Point size = shell.getSize();
		int x = (displayBounds.width - size.x) / 2;
		int y = (displayBounds.height - size.y) / 2;
		shell.setLocation(x, y);
	}

	public void open() {
		while (!shell.isDisposed()) {
			if (isValid) {
				shell.dispose();
				break;
			}
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}